package com.feeling.oafeeling.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.feeling.oafeeling.mapper.MenuRoleMapper;
import com.feeling.oafeeling.mapper.RoleMapper;
import com.feeling.oafeeling.pojo.Role;
import com.feeling.oafeeling.pojo.RoleParam;
import com.feeling.oafeeling.service.RoleService;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private MenuRoleMapper menuRoleMapper;

    @Override
    public List<RoleParam> getAllRole(RoleParam roleParam) {


        System.out.println("1111111111" + roleParam.toString());

        List<RoleParam> allRole = roleMapper.getAllRole(roleParam);
        return allRole;
    }

    @Override
    public List<Role> selectListAll(){
        return roleMapper.selectListAll();
    }

    @Override
    public List<RoleParam> getRoleName() {
        return roleMapper.getRoleName();
    }

    @Override
    public List<String> queryRoleMenu(int roleId) {

        return roleMapper.queryRoleMenu(roleId);
    }

    @Override
    public void delRole(int roleId) {
        roleMapper.delRole(roleId);
    }

    @Override
    public void locksRole(int roleId) {
        roleMapper.locksRole(roleId);
    }

    @Override
    public void updateRoleById(RoleParam roleParam) {
//        for (Long aLong : roleParam.getMenuName()) {
//            roleMapper.updateMenuId(aLong,roleParam.getRoleId());
//        }
//        menuRoleMapper.updateMenuId(roleParam.getRoleId(),roleParam.getMenuName());
        roleMapper.deleteByRoleId(roleParam.getRoleId());
        for (Long aLong : roleParam.getMenuName()) {
            roleMapper.insertRoleId(roleParam.getRoleId(),aLong);
        }
        roleMapper.updateRoleById(roleParam);
    }

    @Override
    public void insertRole(RoleParam roleParam) {
        roleMapper.insertRole(roleParam);
        int roleId = roleMapper.selectByName(roleParam.getRoleName());
        for (Long aLong : roleParam.getMenuName()) {
            roleMapper.insertRoleId(roleId,aLong);
        }
    }
}
